package com.qipay.cpinfo.service.impl;

import com.qipay.baas.model.CpInfo;
import com.qipay.cpinfo.service.CpInfoService;
import com.qipay.mapper.CpInfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CpInfoServiceImpl implements CpInfoService {

    @Autowired
    CpInfoMapper cpInfoMapper;

    @Override
    public List<CpInfo> getByCondition(String name, String legalPerson, String contacts, String contactsMobile, int p, int pz) {
        //String legal_person,String contacts,String contacts_mobile
        p = p <= 0 ? 1 : p;
        pz = pz <= 0 ? 10 : pz;
        Map<String, Object> mapList = buildConditionMap(null, null, null, name, legalPerson, contacts, contactsMobile, null);
        mapList.put("offset", (p - 1) * pz);
        mapList.put("size", pz);
        return cpInfoMapper.selectByCondition(mapList);
    }

    @Override
    public long countByCondition(String name, String legalPerson, String contacts, String contactsMobile) {
        Map<String, Object> mapList = buildConditionMap(null, null, null, name, legalPerson, contacts, contactsMobile, null);
        return cpInfoMapper.countByCondition(mapList);
    }

    @Override
    public CpInfo get(long id) {
        return cpInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public long update(CpInfo cpInfo) {
        if (cpInfo.getCpId() > 0) {
            return cpInfoMapper.updateByPrimaryKey(cpInfo);
        } else {
            return cpInfoMapper.insert(cpInfo);
        }

    }

    @Override
    public int insert(CpInfo cpInfo) {
        return cpInfoMapper.insert(cpInfo);
    }

    @Override
    public int delete(long id) {
        return cpInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<CpInfo> getByConditionAndTypeAgentId(byte type, Long cpId, Long agentId, String name, String legalPerson, String contacts, String contactsMobile, Byte accountState, int p, int pz) {

        p = p <= 0 ? 1 : p;
        pz = pz <= 0 ? 10 : pz;
        Map<String, Object> mapList = buildConditionMap(type, cpId, agentId, name, legalPerson, contacts, contactsMobile, accountState);
        mapList.put("offset", (p - 1) * pz);
        mapList.put("size", pz);
        return cpInfoMapper.selectByAgentId(mapList);


    }

    @Override
    public long countByConditionAndTypeAgentId(byte type, Long cpId, Long agentId, String name, String legalPerson, String contacts, String contactsMobile, Byte accountState) {

        Map<String, Object> mapList = buildConditionMap(type, cpId, agentId, name, legalPerson, contacts, contactsMobile, accountState);
        System.out.println();
        return cpInfoMapper.countByAgentId(mapList);

    }

    @Override
    public List<CpInfo> listAll() {
        return cpInfoMapper.listAll();
    }

    @Override
    public List<CpInfo> listByAgent(Long agentId) {
        return cpInfoMapper.listByAgentId(agentId);
    }

    @Override
    public List<CpInfo> listByCpType(byte type) {
        return cpInfoMapper.listByCpType(type);
    }

    Map<String, Object> buildConditionMap(Byte type, Long cpId, Long agentId, String name, String legalPerson, String contacts, String contactsMobile, Byte accountState) {
        Map<String, Object> mapList = new HashMap<>(7);
        if (type != null) {
            mapList.put("type", type);
        }
        if (cpId != null) {
            mapList.put("cp_id", cpId);
        }

        if (agentId != null && agentId.longValue() > 0) {
            mapList.put("agent_id", agentId);
        }
        if (StringUtils.isNotEmpty((name))) {
            mapList.put("name", name);
        }
        if (StringUtils.isNotEmpty((legalPerson))) {
            mapList.put("legal_person", legalPerson);
        }
        if (StringUtils.isNotEmpty((contacts))) {
            mapList.put("contacts", contacts);
        }
        if (StringUtils.isNotEmpty((contactsMobile))) {
            mapList.put("contacts_mobile", contactsMobile);
        }
        if (accountState != null) {
            mapList.put("account_state", accountState);
        }
        return mapList;
    }
}
